﻿using LibNiconico.NiconicoApi.Lives.Broadcast;
using NicoView.Model.Service.Broadcast;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using NicoView.Model.Service;
using NicoView.Model;
using NicoView.Model.Service.Events;
using GalaSoft.MvvmLight.Messaging;
using NicoView.ViewModel.Message;
using NicoView.ViewModel.Localize;

namespace NicoView.ViewModel
{
    public class NicoViewBroadcastControllerViewModel:NicoViewViewModelBase
    {

        public NicoViewBroadcastControllerViewModel() : this(new NicoViewBroadcastPreset()) { }

        public NicoViewBroadcastControllerViewModel(NicoViewBroadcastPreset preset)
            : this(new NicoViewBroadcastController(preset, NicoViewApplication.Current))
        {

        }

        public NicoViewBroadcastControllerViewModel(NicoViewBroadcastController controller)
        {
            this.Controller = controller;

            this.Controller.PropertyChanged += OnPropertyChanged;


            this.Controller.Preset.PropertyChanged += OnPropertyChanged;

            this.Controller.Queued += Controller_Queued;

            this.addTagCommand = null;

            this.removeTagCommand = null;

            if (this.Tags.Count <= 0)
            {
                this.AddTag();
            }
        }

        void Controller_Queued(NicoViewBroadcastQueueingEventArgs arg)
        {
            this.MessengerInstance.Send(new NicoViewBroadcastQueueingMessage(arg.Count));
        }

        

        internal NicoViewBroadcastController Controller { get; private set; }





        #region 通知プロパティ

        public string Title
        {
            get
            {
               
                return this.Controller.Preset.Title;
            }

            set
            {
                this.Controller.Preset.Title = value;
            }
        }


        public string Description
        {
            get
            {
                

                return this.Controller.Preset.Description;
            }

            set
            {
                this.Controller.Preset.Description = value;
            }
        }

        public ObservableCollection<NicoLiveBroadcastableCommunityInfo> CommunityInfos
        {
            get
            {
                
                return this.Controller.CommunityInfos;
            }

        }

        public NicoLiveBroadcastableCommunityInfo CurrentCommunityInfo
        {
            get
            {
                
                return this.Controller.CurrentCommunityInfo;

            }

            set
            {
                this.Controller.CurrentCommunityInfo = value;
            }
        }


        public PrimaryCategory PrimaryCategory
        {
            get
            {
                if (this.Controller.Preset.PrimaryCategory == LibNiconico.NiconicoApi.Lives.Broadcast.PrimaryCategory.None)
                {
                    this.PrimaryCategory = this.PrimaryCategorys.First();
                }

                return this.Controller.Preset.PrimaryCategory;
            }

            set
            {
                this.Controller.Preset.PrimaryCategory = value;
            }
        }

        private static ObservableCollection<PrimaryCategory> primaryCategorys = null;

        public ObservableCollection<PrimaryCategory> PrimaryCategorys
        {
            get
            {
                if (primaryCategorys == null)
                {
                    primaryCategorys = new ObservableCollection<PrimaryCategory>();
                    foreach (PrimaryCategory primaryCategory in Enum.GetValues(typeof(PrimaryCategory)))
                    {

                        if (primaryCategory != PrimaryCategory.None)
                        {
                            primaryCategorys.Add(primaryCategory);
                        }
                        
                    }
                   
                    

                }

                return primaryCategorys;
            }
        }



        public NicoViewNiconicoTagCollection Tags
        {
            get
            {
                return this.Controller.Preset.Tags;
            }
        }

        

        public bool OutFace
        {
            get
            {
                return this.Controller.Preset.OutFace;
            }

            set
            {
                this.Controller.Preset.OutFace = value;
            }
        }

        public bool AssaultWait
        {
            get
            {
                return this.Controller.Preset.AssaultWait;
            }

            set
            {
                this.Controller.Preset.AssaultWait = value;
            }
        }

        public bool CruiseWait
        {
            get
            {
                
                return this.Controller.Preset.CruiseWait;
            }

            set
            {
                this.Controller.Preset.CruiseWait = value;
            }
        }


        public NicoLivePublicStatus PublicStatus
        {
            get
            {
                return this.Controller.Preset.PublicStatus;
            }

            set
            {
                this.Controller.Preset.PublicStatus = value;
            }
        }


        public bool TimeShiftEnabled
        {
            get
            {
                
                return this.Controller.Preset.TimeShiftEnabled;
            }

            set
            {
                this.Controller.Preset.TimeShiftEnabled = value;
            }
        }

        public bool TwitterEnabled
        {
            get
            {

                return this.Controller.Preset.TwitterEnabled;
            }

            set
            {
                this.Controller.Preset.TwitterEnabled = value;
            }
        }

        public string TwitterTag
        {
            get
            {
                return this.Controller.Preset.TwitterTag;
            }

            set
            {
                this.Controller.Preset.TwitterTag = value;
            }
        }

        


        #endregion


        #region コマンド

        private ICommand addTagCommand;

        public ICommand AddTagCommand
        {
            get
            {                

                if (this.addTagCommand == null)
                {
                    this.addTagCommand = new RelayCommand(() =>
                    {
                        this.AddTag();
                    }, () => this.Controller.Preset.Tags.CanInsertBroadcastTag);

                    
                }

                return this.addTagCommand;
            }
        }


        private ICommand removeTagCommand;

        public ICommand RemoveTagCommand
        {
            get
            {
                if (this.removeTagCommand == null)
                {
                    this.removeTagCommand = new RelayCommand<NicoViewNiconicoTag>((arg) =>
                    {

                        if (this.Tags.Contains(arg))
                        {
                            this.Tags.Remove(arg);
                        }

                    });

                }

                return this.removeTagCommand;
            }
        }
        

       
        

        #endregion


        #region メソッド
        public void AddTag()
        {
            if (this.Controller.Preset.Tags.CanInsertBroadcastTag)
            {
                this.Tags.Add(new NicoViewNiconicoTag());
                this.RaisePropertyChanged(() => this.Tags);
            }
        }

        #endregion

    }
}
